{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "539cc97c-cfc1-447d-8db3-70f312b634c0",
   "metadata": {},
   "source": [
    "# 用于将 聚宽平台 下载的数据，导入到本地数据库中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1cbe299f-7a4b-429e-8c5a-4767100f5316",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-05T03:25:16.718616Z",
     "iopub.status.busy": "2022-05-05T03:25:16.717617Z",
     "iopub.status.idle": "2022-05-05T03:25:18.325373Z",
     "shell.execute_reply": "2022-05-05T03:25:18.324472Z",
     "shell.execute_reply.started": "2022-05-05T03:25:16.717617Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "在使用天勤量化之前，默认您已经知晓并同意以下免责条款，如果不同意请立即停止使用：https://www.shinnytech.com/blog/disclaimer/\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from chanlun.exchange.exchange_db import ExchangeDB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4578d60a-c2d8-4bb3-9b55-ce1498e9656e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-05T03:25:19.016007Z",
     "iopub.status.busy": "2022-05-05T03:25:19.016007Z",
     "iopub.status.idle": "2022-05-05T03:25:19.345867Z",
     "shell.execute_reply": "2022-05-05T03:25:19.344892Z",
     "shell.execute_reply.started": "2022-05-05T03:25:19.016007Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "240819"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ex = ExchangeDB('a')\n",
    "pd_klines = pd.read_csv('./data/stock.csv')\n",
    "len(pd_klines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1f82194c-ce31-4727-8f44-617c64a56587",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-05T03:30:50.440857Z",
     "iopub.status.busy": "2022-05-05T03:30:50.439859Z",
     "iopub.status.idle": "2022-05-05T03:30:50.662021Z",
     "shell.execute_reply": "2022-05-05T03:30:50.662021Z",
     "shell.execute_reply.started": "2022-05-05T03:30:50.440857Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "codes = pd_klines['code'].unique()\n",
    "frequencys = pd_klines['frequency'].unique()\n",
    "pd_klines['date'] = pd.to_datetime(pd_klines['date'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e7886fff-75a7-4236-ac52-64346ee7f45b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-05T03:30:51.714522Z",
     "iopub.status.busy": "2022-05-05T03:30:51.714522Z",
     "iopub.status.idle": "2022-05-05T03:32:13.210367Z",
     "shell.execute_reply": "2022-05-05T03:32:13.209439Z",
     "shell.execute_reply.started": "2022-05-05T03:30:51.714522Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "code SH.000001 frequency w len 882\n",
      "code SH.000001 frequency d len 4209\n",
      "code SH.000001 frequency 30m len 33675\n",
      "code SH.000001 frequency 5m len 202053\n"
     ]
    }
   ],
   "source": [
    "for code in codes:\n",
    "    for f in frequencys:\n",
    "        klines = pd_klines[pd_klines['code'] == code][pd_klines['frequency'] == f]\n",
    "        print(f'code {code} frequency {f} len {len(klines)}')\n",
    "        ex.insert_klines(code, f, klines)\n",
    "print('Done')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c36d6c3-5042-42cb-8949-b9ea74b476c2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}